Library {C}
- parent: 0017 C π
- μ€λͺ : shared libraryλ₯Ό λ§λ€κ³ λ§ν¬νλ λ²μ ν°λνλ€.
- 체ν¬: Done
CMakeλ₯Ό μ¬μ©νλ©΄μ λ―Έλμ μ¬μ©νκ² λ μ¬λ¬ λΌμ΄λΈλ¬λ¦¬λ€μ ν¨κ» μ»΄νμΌνκ³ λ§ν¬νκΈ° μν΄ add_subdirectory(), add_library(), [target_]link_library() λ±λ± ν¨μλ₯Ό μ¬μ©νλ©΄μ λ°μνλ λ¬Έμ λ€μ ν΄κ²°νκΈ° μν΄ μλ‘μ΄ νμ΄μ§λ₯Ό ν λ€.
λͺ©ν
- GCCλ‘ μ μ λΌμ΄λΈλ¬λ¦¬ μ»΄νμΌκ³Ό λ§ν¬λ₯Ό νλ€.
- GCCλ‘ λμ λΌμ΄λΈλ¬λ¦¬ μ»΄νμΌκ³Ό λ§ν¬λ₯Ό νλ€.
static library
νμ¬ νμΌκ΅¬μ‘°λ μλμ κ°λ€. λ κ°μ sample_lib νμΌμ μ»΄νμΌ νμ¬ λΌμ΄λΈλ¬λ¦¬λ‘ λ§λ€κ³ , lib_test_mainμμ λΌμ΄λΈλ¬λ¦¬μ μΈν°νμ΄μ€λ₯Ό κ°μ Έλ€ μΈ κ²μ΄λ€.
.
βββ CMakeLists.txt
βββ lib_test_main.cpp
βββ sample_lib1.cpp
βββ sample_lib2.cpp
// sample_lib1.cpp
int add(const int &a, const int &b)
{
return a + b;
}
// sample_lib2.cpp
int subtract(const int &a, const int &b)
{
return a - b;
}
// lib_test_main.cpp
#include <iostream>
int add(const int &a, const int &b);
int main(void)
{
std::cout << add(1, 2) << "\n";
return 0;
}
λ¨Όμ λ lib νμΌμ μ»΄νμΌνλ€. μ νν λ§νμλ©΄ μ μ²λ¦¬μ μ»΄νμΌ κ³Όμ κΉμ§λ§ μννμ¬ λͺ©μ νμΌμΈ .o νμΌμ λ§λλ κ²μ λͺ©νλ‘ νλ€.
β lib git:(master) β g++ -c sample_lib1.cpp -std=gnu++17 -o sample_lib1.cpp.o
β lib git:(master) β g++ -c sample_lib2.cpp -std=gnu++17 -o sample_lib2.cpp.o
β lib git:(master) β tree
.
βββ CMakeLists.txt
βββ lib_test_main.cpp
βββ sample_lib1.cpp
βββ sample_lib1.cpp.o
βββ sample_lib2.cpp
βββ sample_lib2.cpp.o
0 directories, 6 files
μ΄μ λ¨μν λ λͺ©μ νμΌμ μμΉ΄μ΄λΈ νλ€.
β lib git:(master) β ar -rc MyLib.a *.o
β lib git:(master) β tree
.
βββ CMakeLists.txt
βββ MyLib.a
βββ lib_test_main.cpp
βββ sample_lib1.cpp
βββ sample_lib1.cpp.o
βββ sample_lib2.cpp
βββ sample_lib2.cpp.o
0 directories, 7 files
μ¬μ€ μ΄λ κ² λ¨μν μ λμ λΌμ΄λΈλ¬λ¦¬λ κ΅³μ΄ μμΉ΄μ΄λΈ νμ§ μκ³ ν λ²μ GCC μΈμλ‘ λ£μ΄λ²λ €λ μκ΄μ΄ μμ§λ§ μ μ λΌμ΄λΈλ¬λ¦¬ νμΌ νλλ§ λ€κ³ λ€λλ κ²μ μλ―Έκ° μκΈ° λλ¬Έμ ar 컀맨λλ‘ μμΉ΄μ΄λΈ ν κ²μ΄λ€.
μ΄μ λ©μΈ νλ‘κ·Έλ¨μ μ»΄νμΌνμ. μ μμ μΌλ‘ μ μλνλ€.
β lib git:(master) β g++ -c lib_test_main.cpp -std=gnu++17 -o lib_test_main.cpp.o
β lib git:(master) β g++ -o TestLib.out lib_test_main.cpp.o MyLib.a
β lib git:(master) β ./TestLib.out
3
β lib git:(master) β
CMake with static library
CMakeλ Makeμλ λ€λ₯΄κ² μ€κ°μ λ°μνλ λͺ¨λ λΆμ°λ¬Ό (*.o)λ€μ μΆμννμκΈ° λλ¬Έμ μ¬λ£λ€κ³Ό λ§λ€κ³ μΆμ λΌμ΄λΈλ¬λ¦¬ μ΄λ¦λ§ μμΌλ©΄ μμμ λ§λ€μ΄μ€λ€.
cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_BUILD_TYPE DEBUG)
# μ¬κΈ°μλ λΌμ΄λΈλ¬λ¦¬λ₯Ό λ§λλ μ½λλ§ μμ±ν κ²μ.
project("MyLib")
set(CMAKE_VERBOSE_MAKEFILE TRUE) # project() λͺ
λ Ήμ μ΄ λ³μλ₯Ό νμ FALSEλ‘ μ΄κΈ°ννκΈ°μ...
set(PROJECT_VERSION_MAJOR 1)
set(PROJECT_VERSION_MINOR 0)
set(
LIB_NAME
"${CMAKE_PROJECT_NAME}.${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}"
)
set(
LIB_SOURCES
sample_lib1.cpp
sample_lib2.cpp
)
# λΌμ΄λΈλ¬λ¦¬ λ§λ€κΈ° https://matgomes.com/add-library-cmake-create-libraries/
add_library(${LIB_NAME} STATIC ${LIB_SOURCES})
cmakeλ₯Ό μννκΈ° μν΄ build λλ ν 리λ₯Ό νλ λ§λ€κ³ κ±°κΈ° μμμ cmake λͺ λ Ήμ΄λ₯Ό μΉλ€.
**β lib git:(master) β mkdir build
β lib git:(master) β cd build
β build git:(master) β cmake ..**
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build
λλ κ·Έλ₯ build λλ ν 리λ₯Ό λ§λ€μ§ μκ³ cmake νλΌλ©ν°λ‘ λΉλν λλ ν 리λ₯Ό λͺ μν΄λ λκΈ΄ νλ€.
**β lib git:(master) β cmake -S . -B build**
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build
**β lib git:(master) β tree**
.
βββ CMakeLists.txt
βββ build
β βββ CMakeCache.txt
β βββ CMakeFiles
β β βββ 3.16.3
β β β βββ CMakeCCompiler.cmake
β β β βββ CMakeCXXCompiler.cmake
β β β βββ CMakeDetermineCompilerABI_C.bin
β β β βββ CMakeDetermineCompilerABI_CXX.bin
β β β βββ CMakeSystem.cmake
β β β βββ CompilerIdC
β β β β βββ CMakeCCompilerId.c
β β β β βββ a.out
β β β β βββ tmp
β β β βββ CompilerIdCXX
β β β βββ CMakeCXXCompilerId.cpp
β β β βββ a.out
β β β βββ tmp
β β βββ CMakeDirectoryInformation.cmake
β β βββ CMakeOutput.log
β β βββ CMakeTmp
β β βββ Makefile.cmake
β β βββ Makefile2
β β βββ MyLib.1.0.dir
β β β βββ DependInfo.cmake
β β β βββ build.make
β β β βββ cmake_clean.cmake
β β β βββ cmake_clean_target.cmake
β β β βββ depend.make
β β β βββ flags.make
β β β βββ link.txt
β β β βββ progress.make
β β βββ TargetDirectories.txt
β β βββ cmake.check_cache
β β βββ progress.marks
β βββ Makefile
β βββ cmake_install.cmake
βββ lib_test_main.cpp
βββ sample_lib1.cpp
βββ sample_lib2.cpp
9 directories, 31 files
μ΄μ μ°λ¦¬ νλ‘μ νΈλ₯Ό μν λ©μ΄ν¬νμΌμ μμ±ν΄ μ£ΌμμΌλ make λͺ
λ Ήμ΄λ§ μΉλ©΄ λͺ¨λ λΉλκ° λλλ€. μΌλΆλ‘ CMAKE_VERBOSE_MAKEFILE
μ TRUEλ‘ μ€μ ν΄ μ£ΌμκΈ° λλ¬Έμ μμ² λ§μ λ‘κ·Έκ° μ겨λ¬μ§λ§ μ΄κ²μ λΆμνλ©΄ CMakeκ° μ΄λ»κ² μλνλμ§ λ³Ό μ μλ€.
**β build git:(master) β make**
/usr/bin/cmake -S/home/chltm/workspace/choi-workspace/c++/learning_cmake/lib -B/home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build/CMakeFiles /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build'
make -f CMakeFiles/MyLib.1.0.dir/build.make CMakeFiles/MyLib.1.0.dir/depend
make[2]: Entering directory '/home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build'
cd /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build/CMakeFiles/MyLib.1.0.dir/DependInfo.cmake --color=
Scanning dependencies of target MyLib.1.0
make[2]: Leaving directory '/home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build'
make -f CMakeFiles/MyLib.1.0.dir/build.make CMakeFiles/MyLib.1.0.dir/build
make[2]: Entering directory '/home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build'
[ 33%] Building CXX object CMakeFiles/MyLib.1.0.dir/sample_lib1.cpp.o
**/usr/bin/c++ -g -std=gnu++17 -o CMakeFiles/MyLib.1.0.dir/sample_lib1.cpp.o -c /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/sample_lib1.cpp**
[ 66%] Building CXX object CMakeFiles/MyLib.1.0.dir/sample_lib2.cpp.o
**/usr/bin/c++ -g -std=gnu++17 -o CMakeFiles/MyLib.1.0.dir/sample_lib2.cpp.o -c /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/sample_lib2.cpp**
[100%] Linking CXX static library libMyLib.1.0.a
/usr/bin/cmake -P CMakeFiles/MyLib.1.0.dir/cmake_clean_target.cmake
/usr/bin/cmake -E cmake_link_script CMakeFiles/MyLib.1.0.dir/link.txt --verbose=1
**/usr/bin/ar qc libMyLib.1.0.a CMakeFiles/MyLib.1.0.dir/sample_lib1.cpp.o CMakeFiles/MyLib.1.0.dir/sample_lib2.cpp.o
/usr/bin/ranlib libMyLib.1.0.a**
make[2]: Leaving directory '/home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build'
[100%] Built target MyLib.1.0
make[1]: Leaving directory '/home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build'
/usr/bin/cmake -E cmake_progress_start /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build/CMakeFiles 0
μμ½ν΄λ³΄λ©΄β¦
c++ -g -std=gnu++17 -o CMakeFiles/MyLib.1.0.dir/sample_lib1.cpp.o -c sample_lib1.cpp
c++ -g -std=gnu++17 -o CMakeFiles/MyLib.1.0.dir/sample_lib2.cpp.o -c sample_lib2.cpp
ar qc libMyLib.1.0.a CMakeFiles/MyLib.1.0.dir/sample_lib1.cpp.o CMakeFiles/MyLib.1.0.dir/sample_lib2.cpp.o
/usr/bin/ranlib libMyLib.1.0.a
μ΄μ λλ‘ μμ½ν΄ λ³Ό μ μκ² λ€. μ¬κΈ°μ c++
λͺ
λ Ήμ΄λ g++
λͺ
λ Ήμ΄μ λμΌνλ€ (μ°λΆν¬20.04κΈ°μ€) μμ λΉλν κ²κ³Ό λΉκ΅ν΄λ³Έλ€λ©΄ ar -rc
μ ar qc
μ μ°¨μ΄μΈ λ― νλ€.
-
r
Insert the files member... into archive (with replacement). This operation differs from q in that any previously existing members are deleted if their names match those
being added.νμΌλ€μ μμΉ΄μ΄λΈνλ€. qμμ λ€λ₯Έ μ μ μ΄μ μ λ§λ€μ΄μ§ λͺ¨λ λ©€λ²λ€κ³Ό μ΄λ¦μ΄ κ²ΉμΉλ©΄ μμ λλ€λ κ²μ΄λ€. μ¦, rcλ qcλ λκ°μ΄ μμΉ΄μ΄λΈ νλ λͺ λ Ήμ΄μ΄λ€.
-
ranlib
μμΉ΄μ΄λΈμ λν μΈλ±μ€λ₯Ό λ§λλ λͺ λ Ήμ΄. μΈλ±μ± μλλ₯Ό λμΌ μ μλ€κ³ νλ€. μμ΄λ 무κ΄ν κ² κ°λ€.
κ²°λ‘ μ μΌλ‘ νμΌμ΄ μμ²λκ² λ§μ΄ μ겨λκΈ΄ νμ§λ§ μ°λ¦¬λ build/{νκ²μ΄λ¦}λ§ λ³΄λ©΄ λλ€. μ°λ¦¬λ libMyLib.1.0.a
νμΌμ΄ μ΅μ’
νκ²μ΄λ€. λ§μ½ μ΄ μμΉ΄μ΄λΈλ₯Ό λ©μΈν¨μκ° λ¬λ¦° νλ‘κ·Έλ¨μ λ§ν¬μν€λ €λ©΄ μμμ ν κ²κ³Ό λκ°μ΄ νλ©΄ λλ€.
β lib git:(master) β g++ lib_test_main.cpp build/libMyLib.1.0.a
β lib git:(master) β ./a.out
3
shared library
μ΄λ²μ λμ λΌμ΄λΈλ¬λ¦¬λ₯Ό λ§λ€μ΄ λΉλκ³Όμ μ μ΄λ»κ² ν¬ν¨λλμ§ μμλ³Ό κ²μ΄λ€. λ¨Όμ μμ μμ νλ λ°©λ²μ λ¨Όμ ν΄λ³΄μ. https://medium.com/meatandmachines/shared-dynamic-libraries-in-the-c-programming-language-8c2c03311756 λ₯Ό μ°Έκ³ νλ€.
β lib git:(master) β g++ -g -fPIC -Wall -Werror -Wextra -pedantic sample_lib*.cpp -shared -o libMyLib.so
β lib git:(master) β ls
CMakeLists.txt libMyLib.so lib_test_main.cpp sample_lib1.cpp sample_lib2.cpp
β lib git:(master) β g++ -g -Wall lib_test_main.cpp libMyLib.so
β lib git:(master) β l
total 84
drwxr-xr-x 2 chltm chltm 4096 Jul 18 18:13 ./
drwxr-xr-x 6 chltm chltm 4096 Jul 18 17:15 ../
-rw-r--r-- 1 chltm chltm 631 Jul 18 17:14 CMakeLists.txt
-rwxr-xr-x 1 chltm chltm 39584 Jul 18 18:13 a.out*
-rwxr-xr-x 1 chltm chltm 18800 Jul 18 18:07 libMyLib.so*
-rw-r--r-- 1 chltm chltm 142 Jul 18 16:05 lib_test_main.cpp
-rw-r--r-- 1 chltm chltm 75 Jul 18 16:06 sample_lib1.cpp
-rw-r--r-- 1 chltm chltm 60 Jul 18 17:08 sample_lib2.cpp
β lib git:(master) β ./a.out
./a.out: error while loading shared libraries: libMyLib.so: cannot open shared object file: No such file or directory
https://www.lesstif.com/lpt/shared-library-dependencies-linux-ldd-95880421.html
ldd λͺ λ Ήμ΄λ₯Ό μ¬μ©νλ©΄ νμν 곡μ λΌμ΄λΈλ¬λ¦¬μ μμ‘΄μ±μ μΆλ ₯ν΄ μ€λ€κ³ νλ€. νμ¬ a.out μ μμ‘΄μ±μ νμΈν΄λ³΄μ.
β lib git:(master) β ldd ./a.out
linux-vdso.so.1 (0x00007fffb64d5000)
libMyLib.so => not found
libstdc++.so.6 => /lib/x86_64-linux-gnu/libstdc++.so.6 (0x00007f8a7b103000)
libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f8a7b0e8000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f8a7aef6000)
libm.so.6 => /lib/x86_64-linux-gnu/libm.so.6 (0x00007f8a7ada7000)
/lib64/ld-linux-x86-64.so.2 (0x00007f8a7b2f7000)
νμΌ μμΉλ₯Ό νμΈν μ μλ€κ³ ? λ°λ‘ μμ λκ³ λ λͺ» μ°Ύλλ€κ³ ?
https://stackoverflow.com/a/8538605/6428901 μ λ°λ₯΄λ©΄, μ€ν λ€λ μμ€ν
λΌμ΄λΈλ¬λ¦¬ (λμ κ²½μ°λ /lib/x86_64-linux-gnu/
)κ° μλ κ²½μ°λ νκ²½λ³μμ ν΄λΉ 곡μ λΌμ΄λΈλ¬λ¦¬μ μμΉλ₯Ό μΆκ°νλ κ²μΌλ‘ λ¬Έμ λ₯Ό ν΄κ²°ν μ μλ€κ³ νλ€.
β lib git:(master) β export LD_LIBRARY_PATH=.
β lib git:(master) β ./a.out
3
μ λλ€! νκ°μ§ κ±±μ μ, μ΄ μΈμμ μΌλ§λ λ§μ λμ λΌμ΄λΈλ¬λ¦¬λ€μ΄ λΆμ νκ³ λ μλ‘ μ½νκ³ μ₯νλλ°, νλ‘κ·Έλ¨ μμ λμ λΌμ΄λΈλ¬λ¦¬λ₯Ό μ§μ μμΉν μ μλ€λ©΄ λ§€λ² μ΄λ κ² νκ²½λ³μ μ€μ μ ν΄μ£Όκ³ μμ‘΄μ± κ΄λ¦¬λ₯Ό ν΄μ£Όμ΄μΌ νλ κ±ΈκΉ?
CMake with shared library
CMake with static library μ μ μλ μ½λμ λ§μ§λ§ μ€μ STATIC
ν€μλλ₯Ό SHARED
ν€μλλ‘ λ°κΎΈκΈ°λ§ νλ©΄ λλ€.
# static library
add_library(${LIB_NAME} STATIC ${LIB_SOURCES})
# shared library
add_library(${LIB_NAME} SHARED ${LIB_SOURCES})
**β lib git:(master) β cmake -S . -B build**
-- The C compiler identification is GNU 9.4.0
-- The CXX compiler identification is GNU 9.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build
**β lib git:(master) β make --directory=build**
make: Entering directory '/home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build'
/usr/bin/cmake -S/home/chltm/workspace/choi-workspace/c++/learning_cmake/lib -B/home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build/CMakeFiles /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build'
make -f CMakeFiles/MyLib.1.0.dir/build.make CMakeFiles/MyLib.1.0.dir/depend
make[2]: Entering directory '/home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build'
cd /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build/CMakeFiles/MyLib.1.0.dir/DependInfo.cmake --color=
Scanning dependencies of target MyLib.1.0
make[2]: Leaving directory '/home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build'
make -f CMakeFiles/MyLib.1.0.dir/build.make CMakeFiles/MyLib.1.0.dir/build
make[2]: Entering directory '/home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build'
[ 33%] Building CXX object CMakeFiles/MyLib.1.0.dir/sample_lib1.cpp.o
**/usr/bin/c++ -DMyLib_1_0_EXPORTS -g -fPIC -std=gnu++17 -o CMakeFiles/MyLib.1.0.dir/sample_lib1.cpp.o -c /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/sample_lib1.cpp**
[ 66%] Building CXX object CMakeFiles/MyLib.1.0.dir/sample_lib2.cpp.o
**/usr/bin/c++ -DMyLib_1_0_EXPORTS -g -fPIC -std=gnu++17 -o CMakeFiles/MyLib.1.0.dir/sample_lib2.cpp.o -c /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/sample_lib2.cpp**
[100%] Linking CXX shared library libMyLib.1.0.so
/usr/bin/cmake -E cmake_link_script CMakeFiles/MyLib.1.0.dir/link.txt --verbose=1
**/usr/bin/c++ -fPIC -g -shared -Wl,-soname,libMyLib.1.0.so -o libMyLib.1.0.so CMakeFiles/MyLib.1.0.dir/sample_lib1.cpp.o CMakeFiles/MyLib.1.0.dir/sample_lib2.cpp.o**
make[2]: Leaving directory '/home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build'
[100%] Built target MyLib.1.0
make[1]: Leaving directory '/home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build'
/usr/bin/cmake -E cmake_progress_start /home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build/CMakeFiles 0
make: Leaving directory '/home/chltm/workspace/choi-workspace/c++/learning_cmake/lib/build'
μ¬κΈ°μμ λμ¬κ²¨ 보μμΌ ν λΆλΆλ§ μμ νμλ©΄β¦
c++ -DMyLib_1_0_EXPORTS -g -fPIC -std=gnu++17 -o sample_lib1.cpp.o -c sample_lib1.cpp
c++ -DMyLib_1_0_EXPORTS -g -fPIC -std=gnu++17 -o sample_lib2.cpp.o -c sample_lib2.cpp
c++ -fPIC -g -shared -Wl,-soname,libMyLib.1.0.so -o libMyLib.1.0.so sample_lib1.cpp.o sample_lib2.cpp.o
첫λ²μ§Έ, λ λ²μ§Έ μ€μ κ²½μ° -D
μ΅μ
μ ν΅ν΄μ Define ꡬ문μ μΆκ°ν κ² λ§κ³€ νΉλ³ν λ€λ₯Έ μ μ μλ κ² κ°λ€. λ¬Έμ λ μΈ λ²μ§Έ μ€μμ λ κ°μ λͺ©μ νμΌμ ν΅ν΄ λμ λΌμ΄λΈλ¬λ¦¬ libMyLib.1.0.so
λ₯Ό λ§λλ λ° λ€μ΄κ° νλΌλ©ν°μΈλ°, λͺ¨λ₯΄λ κ²λ€μ΄ λ§λ€.
-
-Wl,
https://gcc.gnu.org/onlinedocs/gcc-11.2.0/gcc/Link-Options.htmlPass option as an option to the linker. If option contains commas, it is split into multiple options at the commas. You can use this syntax to pass an argument to the option. For example, -Wl,-Map,output.map passes -Map output.map to the linker. When using the GNU linker, you can also get the same effect with -Wl,-Map=output.map.
comma λ€μ λΆμ λͺ¨λ λͺ λ Ήμ΄λ€μ
ld
λ§μ»€μκ² λκΈ°κ² λ€λ μλ―Έμ΄λ€. -
-soname,
https://www.man7.org/linux/man-pages/man1/ld.1.htmlWhen creating an ELF shared object, set the internal DT_SONAME field to the specified name. When an executable is linked with a shared object which has a DT_SONAME field, then
when the executable is run the dynamic linker will attempt to load the shared object specified by the DT_SONAME field rather than the using the file name given to the linker.ld
λͺ λ Ήμ΄μ νλΌλ©ν°λ‘, λμ λΌμ΄λΈλ¬λ¦¬μ νμΌμ΄λ¦λ³΄λ€ μ°μ νλ μ΄λ¦μ μ§μ ν μ μλ€. μμΌλ©΄ νμΌ μ΄λ¦μ λ°λΌκ°κ³ μμΌλ©΄ λ°λμ sonameκ³Ό μΌμΉνλ λμ λΌμ΄λΈλ¬λ¦¬λ₯Ό λ‘λ. -
-shared
https://gcc.gnu.org/onlinedocs/gcc/Link-Options.htmlProduce a shared object which can then be linked with other objects to form an executable. Not all systems support this option. For predictable results, you must also specify the same set of options used for compilation (-fpic, -fPIC, or model suboptions) when you specify this linker option.1
λμ λΌμ΄λΈλ¬λ¦¬λ₯Ό λ§λ€μ΄μ£Όλ μ΅μ . λͺ¨λ μμ€ν μ΄ μ΄λ₯Ό μ§μνμ§λ μκΈ° λλ¬Έμ μ¬μ€ λ νμ€ν
-fPIC
μ΅μ μ μ¬μ©νμ.κ·Έλ¬λ©΄ CMakeλ κ΅³μ΄ νμλ μλ μ΅μ μ μ μΆκ°νμκΉ?
λμ λΌμ΄λΈλ¬λ¦¬λ₯Ό λ§ν¬νμ¬ λ©μΈ νλ‘κ·Έλ¨μ λΉλν΄λ³΄μ.
β lib git:(master) β g++ lib_test_main.cpp build/libMyLib.1.0.so
β lib git:(master) β ./a.out
./a.out: error while loading shared libraries: libMyLib.1.0.so: cannot open shared object file: No such file or directory
μκΉμ λκ°μ μλ¬κ° λμλ€. μ΄μ ν΄κ²°ν μ μλ κ°λ¨ν λ¬Έμ .
β lib git:(master) β echo $LD_LIBRARY_PATH
.
β lib git:(master) β export LD_LIBRARY_PATH=.:./build:
β lib git:(master) β ./a.out
3